﻿using Company.Core.Interfaces;
using Company.Shell.Interfaces;
using Company.Shell.Models;

namespace Company.Shell.Services
{
    public class CacheManager : ICacheManager
    {
        public List<CacheEntity> Caches { get; private set; } = [];
        private IConfigManager ConfigManager { get;  }
        public CacheManager(IConfigManager configManager )
        {
            configManager = configManager ?? throw new ArgumentNullException( nameof( configManager ) );
            ConfigManager = configManager;
            Initialize();
        }

        private void Initialize()
        {
            Caches = ConfigManager.Read<List<CacheEntity>>(Core.Enums.ConfigType.Cache);//读取缓存
            if(Caches == null)
            {
                Caches = [];
                ConfigManager.Write(Core.Enums.ConfigType.Cache, Caches);//写入空缓存
            }
        }

        public void AddCache(CacheEntity entity)
        {
            Caches.Add(entity);
        }

        public void Save()
        {
            ConfigManager.Write(Core.Enums.ConfigType.Cache, Caches);
        }
    }
}
